<?php
namespace App\Services\Message;
use App\Models\User\User;
use App\Services\Message\Message\MessageLogic;
/**
 * @Author: ws
 * @Date:   2018-06-12 16:23:45
 * @Last Modified by:   Marte
 */
class Message
{

    /*一对一发送站内信*/
    public function sendOneMessage($data): array
    {
        if (empty($data)) {
            return null_data($data,"参数不能为空！");
        }
        if (!isset($data['to_uid']) && isset($data['to_name'])) {
            $toUser = User::where('name', $data['to_name'])->first();
            if (!$toUser) {
                return auto_failure(null, '改用户不存在');
            }
            $data['to_uid'] = $toUser->id;
            $data['orther_info'] = $toUser->other_info;
            unset($data['to_name'], $data['token']);
        }

        $data = init_array($data,['from_uid'=>0,'to_uid'=>0,'title'=>' ','content'=>' ','type'=>' ','orther_info'=>'']);
        $validateInitData = ['to_uid' => true,'orther_info' => true,'action' => 'sendOneMessage'];
        ValidateMessage::checkValidate($data, ['from_uid','to_uid','title','content','type'], true, $validateInitData);
        return (new MessageLogic())->sendBatchMessage($data);
    }

    /*一对多发送站内信*/
    public function sendBatchMessage($data): array
    {
        if(in_array($data['from_uid'], $data['to_uid'])){
             return null_data($data,"from_uid 不应该在 to_uid 数组里");
        }
        $data = init_array($data,['from_uid'=>0,'to_uid'=>[],'title'=>' ','content'=>' ','type'=>' ','orther_info'=>'']);
        $validateInitData = ['to_uid' => $data['to_uid'],'orther_info' => true,'action' => 'sendBatchMessage'];
        ValidateMessage::checkValidate($data, ['from_uid','title','content','type'], true, $validateInitData);
        return (new MessageLogic())->sendBatchMessage($data);
    }


    //用户操作站内信时批量检验 接收到的数据、站内信、用户
    protected function _checkBatchUserMsg($data, $operate = ''): array
    {
        is_numeric($data['id']) && $data['id'] = [$data['id']];
        $messages = (new MessageLogic())->dbMessageList($data);
        if(!$messages){
            return verify_err(null,'要操作的站内信不存在');
        }
        if(count($messages) != count($data['id'])){
            return verify_err(null, '批量选择的站内信与查询到的站内信个数不一致');
        }
        foreach ($messages as $k => $message) {

            $errMsg = 'ID为' . $message['id'] . '的站内信';

            if( !in_array($data['user_id'], [$message['from_uid'],$message['to_uid']]) ){
                return verify_err($message, $errMsg . '不属于您');
            }
            if( $data['user_id'] == $message['from_uid'] && !in_array($message['is_del'], [0,2]) ){
                return verify_err($message, $errMsg . '已被删除');
            }
            if( $data['user_id'] == $message['to_uid'] && !in_array($message['is_del'], [0,1]) ){
                return verify_err($message, $errMsg . '已被删除');
            }
            if($operate == 'receive'){
                if($data['user_id'] != $message['to_uid']){
                    return verify_err($message, $errMsg . ',您不是接收方，不可操作');
                }
            }
            if($operate == 'send'){
                if($data['user_id'] != $message['from_uid']){
                    return verify_err($message, $errMsg . ',您不是发送方，不可操作');
                }
            }
        }

        return auto_success($message, '该用户可操作该站内信');

    }

    /*管理员查看某站内信详情*/
    public function adminMessageDetail($data): array
    {
        $data = init_array($data, ['id' => 0]);
        ValidateMessage::checkValidate($data, ['id']);
        return (new MessageLogic())->messageDetail($data);
    }

    /*用户查看某站内信详情*/
    public function messageInfo($data): array
    {

        $data = init_array($data, ['id'=>0,'user_id'=>0]);
        $checkUserRes = $this->_checkBatchUserMsg($data, 'checkIdUid');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }
        return (new MessageLogic())->messageDetail($data);

    }

    /*查看某用户的收件箱、发件箱*/
    public function messageList($data): array
    {
        $data = init_array($data, ['user_id'=>0,'msgType'=>' ']);
        ValidateMessage::checkValidate($data, ['user_id']);
        return (new MessageLogic())->messageList($data);
    }

     /*管理员查看某用户的收件箱、发件箱*/
    public function adminMessageList($data): array
    {
        $data = init_array($data, ['keywords'=>'']);
        return (new MessageLogic())->adminMessageList($data);
    }

    /*将站内信设为已读*/
    public function markRead($data): array
    {
        $checkUserRes = $this->_checkBatchUserMsg($data, 'receive');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }
        return (new MessageLogic())->setRead($data);
    }

    /*获取某用户未读站内信个数*/
   /* public function noReadCount($data): array
    {
        $data = $this->receiveData(['user_id'=>0]);
        return $this->stepRun($data, 'noReadCount', 'checkUid');
    }*/

    /*将某站内信设为已读*/
   /* public function setOneRead($data): array
    {
        $data = $this->receiveData(['id'=>0,'user_id'=>0]);
        return $this->_setBatchRead($data, 'checkIdUid');
    }*/

    /*将多个站内信设为已读*/
    /*public function setBatchRead($data): array
    {
        $data = $this->receiveData(['id'=>[],'user_id'=>0]);
        return $this->_setBatchRead($data, 'checkIdsUid');
    }*/

    /*将站内信设为已读*/
    /*protected function _setBatchRead($data, $scene): array
    {

        $checkUserRes = $this->_checkBatchUserMsg($data, $scene, 'receive');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }

        return $this->logic($this->messageLogic)->setRead($data);
    }*/

    /*用户回复某个站内信*/
    public function replyOneMessage($data): array
    {
        if (empty($data)) {
            return null_data($data,"参数不能为空！");
        }
        $data = init_array($data,['id'=>0,'user_id'=>0,'content'=>' ','orther_info'=>'{}']);
        $validateInitData = ['orther_info' => true,'action' => 'replyOneMessage'];
        ValidateMessage::checkValidate($data, ['id','user_id','content'], true, $validateInitData);
        $checkUserRes = $this->_checkBatchUserMsg($data, 'receive');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }
        return (new MessageLogic())->replyOneMessage($data);

    }

    /*用户删除某个站内信*/
    public function delOneMessage($data): array
    {
        if (empty($data)) {
            return null_data($data,"参数不能为空！");
        }
        $data = init_array($data,['id'=>0,'user_id'=>0]);
        $validateInitData = ['id' => true,'user_id' => true];
        ValidateMessage::checkValidate($data, ['id','user_id'], true, $validateInitData);

        $checkUserRes = $this->_checkBatchUserMsg($data, 'checkIdUid');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }
        return (new MessageLogic())->delOneMessage($data);

    }

    /*用户批量删除站内信*/
   /* public function delBatchMessage($data): array
    {
        $data = $this->receiveData(['id'=>[],'user_id'=>0]);

        $checkUserRes = $this->_checkBatchUserMsg($data, 'checkIdsUid');
        if($checkUserRes['status'] != SUCCESS_STATUS){
            return $checkUserRes;
        }

        return $this->logic($this->messageLogic)->delBatchMessage($data);

    }*/

    /*管理员删除某个站内信*/
    public function adminDelOneMessage($data): array
    {
        $data = init_array($data, ['id' => 0]);
        ValidateMessage::checkValidate($data, ['id']);
        return (new MessageLogic())->adminDelMessage($data);

    }

    /*管理员批量删除某个站内信*/
    public function adminDelBatchMessage($data): array
    {
        $data = init_array($data, ['id'=>[]]);
        $validateInitData = ['id' => true,'action'=> 'adminDelBatchMessage'];
        ValidateMessage::checkValidate($data, ['id'], true, $validateInitData);
        return (new MessageLogic())->adminDelMessage($data);
    }
}
